<template>
  <div class="app-container">

    <el-card>

      <el-form :model="regParams" size="small" :inline="true"  label-width="90px">
        <el-form-item label="挂号id：" prop="registerId">
          <el-input
            v-model="regParams.registerId"
            placeholder="请输入挂号id"
            clearable
            @keyup.enter.native="regQuery"
          />
        </el-form-item>

        <el-form-item label="病人姓名：" prop="patientName">
          <el-input
            v-model="regParams.patientName"
            placeholder="请输入病人姓名"
            
            clearable
            @keyup.enter.native="regQuery"
          />
        </el-form-item>
          <!-- @keyup.enter.native 回车自动调用方法 -->

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="regQuery">搜索</el-button>
        </el-form-item>

        <el-table v-loading="presLoading" :data="prescriptionList">
          <el-table-column label="处方id" align="center" prop="prescriptionId" width="70"/>
          <el-table-column label="挂号id" align="center" prop="registerId" width="70"/>
          <el-table-column label="病人姓名" align="center" prop="patientName" width="100"/>
          <el-table-column label="科室名称" align="center" prop="depName" width="100"/>
          <el-table-column label="日期" align="center" prop="prescriptionDate" width="100">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.prescriptionDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="医生姓名" align="center" prop="userName" width="100"/>
          <el-table-column label="药品及用量" align="center" prop="rpMedicine" />
          <el-table-column label="检查项目" align="center" prop="rpInspect" />
          <el-table-column label="临床诊断" align="center" prop="diagnosis" />
          <el-table-column label="备注" align="center" prop="remark" />
          
        </el-table>

      </el-form>
    
    </el-card><br>

    <el-card>
          <el-form :model="medNameParams" size="small" :inline="true"  label-width="68px">
            <el-form-item label="药名:" prop="medicineName">
              <el-input
                v-model="medNameParams.medicineName"
                placeholder="请输入药品名"
                clearable
                @keyup.enter.native="medNameQuery"
              />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="medNameQuery">搜索</el-button>
            </el-form-item>

            

            <el-table v-loading="medLoading" :data="medicineList">
              <el-table-column label="药品id" align="center" prop="medicineId" v-if="true"/>
              <el-table-column label="药品名" align="center" prop="medicineName" />
              <el-table-column label="药品单价" align="center" prop="medicinePrice" />
              <el-table-column label="药品库存" align="center" prop="medicineStock" />
              <el-table-column label="注意事项" align="center" prop="medicineDetails" />
              <el-table-column label="购药数量" align="center" class-name="small-padding fixed-width"> 
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.medicineNum"
                    placeholder="请输入购药数量"
                    clearable
                    style="width:180px;"
                  />
                </template>
              </el-table-column>
              <el-table-column
                fixed="right"
                label="操作"
                width="120">
                <template slot-scope="scope">
                  <el-button
                    @click.native.prevent="deleteRow(scope.$index, medicineList)"
                    type="text"
                    size="small">
                    移除
                  </el-button>
                </template>
              </el-table-column>
            </el-table><br>

            <el-form-item label="处方id：" prop="prescriptionId">
              <el-input
                v-model="orderParams.prescriptionId"
                placeholder="请输入处方id"
                clearable
              />
            </el-form-item>
     

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="buyMed">购药</el-button>
            </el-form-item>

          </el-form>





    </el-card><br>






    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item prop="moId">
          <el-input
            v-model="queryParams.moId"
            placeholder="请输入药品订单id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['reghos-serv:order:add']"
          >新增</el-button>
        </el-col> -->
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['reghos-serv:order:edit']"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['reghos-serv:order:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['reghos-serv:order:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="药品订单id" align="center" prop="moId" v-if="true"/>
        <el-table-column label="药品id" align="center" prop="medicineId" />
        <el-table-column label="药品数量" align="center" prop="medicineNum" />
        <el-table-column label="药品单价" align="center" prop="medicinePrice" />
        <el-table-column label="订单总价" align="center" prop="moTotalPrice" />
        <el-table-column label="订单时间" align="center" prop="moDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.moDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="处方id" align="center" prop="prescriptionId" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['reghos-serv:order:edit']"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['reghos-serv:order:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card><br>




    






    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品id" prop="medicineId">
          <el-input v-model="form.medicineId" placeholder="请输入药品id" />
        </el-form-item>
        <el-form-item label="药品数量" prop="medicineNum">
          <el-input v-model="form.medicineNum" placeholder="请输入药品数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处方id" prop="prescriptionId">
          <el-input v-model="form.prescriptionId" placeholder="请输入处方id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
    <!-- 订单详情对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品名称" prop="medicineId">
          <el-input v-model="form.medicineName" placeholder="请输入药品id" />
        </el-form-item>
        <el-form-item label="药品数量" prop="medicineNum">
          <el-input v-model="form.medicineNum" placeholder="请输入药品数量" />
        </el-form-item>
        <el-form-item label="药品单价" prop="medicinePrice">
          <el-input v-model="form.medicinePrice" placeholder="请输入药品单价" />
        </el-form-item>
        <el-form-item label="订单总价" prop="moTotalPrice">
          <el-input v-model="form.moTotalPrice" placeholder="请输入订单总价" />
        </el-form-item>
        <el-form-item label="订单时间" prop="moDate">
          <el-date-picker clearable
            v-model="form.moDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择订单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入处方id" />
        </el-form-item>
        <el-form-item label="医生名称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入处方id" />
        </el-form-item>
      </el-form>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder ,queryPrescription, getPres ,getMedicines,buyMed} from "@/api/reghos-serv/order";

export default {
  name: "Order",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      presLoading:false,
      medLoading:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      title2:"",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams2:{
        prescriptionId:""
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      regParams:{
        registerId:"",
        patientName:"",
      },
      medNameParams:{
        medicineName:"",
      },
      medicineList: [],
      prescriptionList: [],
      orderParams:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询药品订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        moId: undefined,
        medicineId: undefined,
        medicineNum: undefined,
        medicinePrice: undefined,
        moTotalPrice: undefined,
        moDate: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        prescriptionId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.moId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      
      // this.loading = true;
      // queryPrescription(this.queryParams2.prescriptionId).then(response => {
      //   this.form = response.rows;
   



        
      // });
      this.open = true;
      this.loading = false;
      this.title = "添加药品订单";
      this.reset();
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const moId = row.moId || this.ids
      getOrder(moId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open2 = true;
        this.title2 = "药品订单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.moId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const moIds = row.moId || this.ids;
      this.$modal.confirm('是否确认删除药品订单编号为"' + moIds + '"的数据项？').then(() => {
        this.loading = true;
        return delOrder(moIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('reghos-serv/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    regQuery(){
      this.presLoading = true;
      getPres(this.regParams.registerId,this.regParams.patientName).then(response => {
        console.log(response);
        this.prescriptionList = response.data.prescriptionList;
        
      }).finally(() => {
        this.presLoading = false;
        this.regParams.registerId = "";
        this.regParams.patientName = ""; 
      });
    },
    medNameQuery(){
      if(this.medNameParams.medicineName == null || this.medNameParams.medicineName == ""){
        this.$message.error("请输入查询药品的名称！");
        return;
      }
      this.medLoading = true;
      getMedicines(this.medNameParams.medicineName).then(response => {
        if(this.medicineList.length == 0){
          response.data.medicineList.forEach((value , index) => {
            value['medicineNum'] = "";
          })
          console.log(response.data.medicineList);
          this.medicineList = response.data.medicineList;
        }else{
          this.medicineList = this.medicineList.concat(response.data.medicineList);          
        }
          
      }).finally(() => {
        this.medLoading = false;
        this.medNameParams.medicineName = "";
      });
    },
    buyMed(){     
      if(this.orderParams.prescriptionId == null){
        this.$message.error("请输入处方号！");
        return;
      }
      console.log(this.medicineList);
      this.medLoading = true;
      buyMed(this.medicineList,this.orderParams.prescriptionId).then(response => {
        this.medicineList = [];
        this.orderParams.prescriptionId = "";
        this.getList();
          
      }).finally(() => {
        this.medLoading = false;
        this.$message.success("购药成功！");
      });
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    }
  }
};
</script>
